//
//  HTPieChart.m
//  eduBoat
//
//  Created by meet on 14-3-13.
//  Copyright (c) 2014年 MEETStudio. All rights reserved.
//

#import "HTPieChart.h"

@implementation HTPieChart

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.layer.shadowColor = [[UIColor blackColor] CGColor];
        self.layer.shadowOffset = CGSizeMake(0.0f, 2.5f);
        self.layer.shadowRadius = 1.9f;
        self.layer.shadowOpacity = 0.9f;
    }
    return self;
}

- (void)reloadData
{
    [self setNeedsDisplay];
}


- (void)drawRect:(CGRect)rect
{
    
    CGFloat space =3;
    CGFloat finishProportion = [self finishProportion];
    CGFloat startAngle = M_PI;
    CGFloat endAngle = (finishProportion<=0.5)?((finishProportion*2+1)*M_PI):(finishProportion*2-1)*M_PI;
    CGFloat centerX = rect.size.width/2;
    CGFloat centerY = rect.size.height/2;
    CGFloat radius = rect.size.width/2;
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextAddArc(context,centerX , centerY, radius/2, startAngle,endAngle, 0);
    CGContextSetStrokeColorWithColor(context, [self finishPartColor].CGColor);
    CGContextSetLineWidth(context, radius);
    CGContextStrokePath(context);
    
    CGContextAddArc(context, centerX, centerY, radius/2-space, endAngle , startAngle, 0);
    CGContextSetStrokeColorWithColor(context, [self unfinishPartColor].CGColor);
    CGContextSetLineWidth(context, radius-2*space);
    CGContextStrokePath(context);

    
}


#pragma mark - 代理的值

- (CGFloat)finishProportion
{
    CGFloat finishPart = 0;
    if(_dataSource && [_dataSource respondsToSelector:@selector(theFinishProportionInPieChart:)]){
        
        finishPart = [_dataSource theFinishProportionInPieChart:self];
    }
    
    return finishPart;
}

- (UIColor *)finishPartColor
{
    UIColor *finishColor;
    
    if(_dataSource && [_dataSource respondsToSelector:@selector(theFinishProportionColorInPieChart:)]){
        
        finishColor = [_dataSource theFinishProportionColorInPieChart:self];
    }else{
        
        finishColor = [UIColor orangeColor];
    }
    
    return finishColor;
}

- (UIColor *)unfinishPartColor
{
    
    UIColor *unfinishColor;
    
    if(_dataSource && [_dataSource respondsToSelector:@selector(theUnfinishProportionColorInPieChart:)]){
        
        unfinishColor = [_dataSource theUnfinishProportionColorInPieChart:self];
    }else{
        
        unfinishColor = [UIColor redColor];
    }
    
    return unfinishColor;
}

@end
